package com.wz.jiangsu.main;

import java.time.Duration;
import java.time.LocalDateTime;
import java.time.Period;
import java.time.format.DateTimeFormatter;

public class DateTimeDifferenceExample {

    public static void main(String[] args) {
        // 获取当前日期时间
        LocalDateTime currentDateTime = LocalDateTime.now();

        // 假设过去的日期时间为 "2023-01-01T12:00:00"
        String pastDateTimeString = "2024-01-01T12:00:00";
        LocalDateTime pastDateTime = LocalDateTime.parse(pastDateTimeString, DateTimeFormatter.ISO_LOCAL_DATE_TIME);

        // 计算日期时间差异
        Duration duration = Duration.between(pastDateTime, currentDateTime);

        // 获取相差的天数和剩余的秒数
        Period period = duration.toDays() > 0 ? Period.ofDays((int) duration.toDays()) : Period.ZERO;
        duration = duration.minusDays(period.getDays());

        // 获取相差的小时和剩余的秒数
        long hours = duration.toHours();
        duration = duration.minusHours(hours);



        // 输出结果
        System.out.println("相差：" + period.getYears() + " 年 " + period.getMonths() + " 月 " + period.getDays() + " 天 "
                + hours + " 小时 " );
    }
}
